
    function validOctetMask(octet){
        switch (octet){
            case 0:
            case 128:
            case 192:
            case 224:
            case 240:
            case 248:
            case 252:
            case 254:
            case 255: return true;
            default: return false;
        }   
    } 
     
    function getNetwork(ip,subnet)
    {
        ip = ip.split(".");
        subnet = subnet.split(".");
        network = '';
        lastOctet = 255;
        
        for (i=0;i<4;++i){  
            ip[i]=parseInt(ip[i]);
            subnet[i]=parseInt(subnet[i]);
            
            // This octet must be 0 of the previous was less than 255.
            if ( lastOctet < 255 && subnet[i] !=0 )
                return -101; // Error
            
            // Determine if this octet's mask is valid
            if (!validOctetMask(subnet[i]))
                return -102; // Error
            
            if (subnet[i]==0) {           // Obvious ones
                network += '0';  
            } else if (subnet[i]==255){   // Obvious ones
                network += ip[i]; 
            } else if (subnet[i]==254 ){    // Weird one
                if (i==3)
                    return -103;

                network += ip[i] - (ip[i]%2)
            } else {
                // Determine how many addresses this allowed,
                HOSTSPERSUBNET=254-subnet[i]
                TOTALSUBNETS = 255/HOSTSPERSUBNET
                
                // and which part of the range our IP belongs to...
                for (c=0;c<parseInt(TOTALSUBNETS);++c){           // FIXME: This is where we're liable to be off by X
                    if ( ip[i] >= c*HOSTSPERSUBNET+c*2)
                        netOctet = c*HOSTSPERSUBNET+c*2;    // It's okay to keep rewriting this until the condition fails
                }
                network += netOctet;   
            }
            if (i<3) network += '.';
            lastOctet = subnet[i];            
        }
        return network;
    } 

    function getBroadcast(network,subnet)
    {
        network = network.split(".");
        subnet = subnet.split(".");
        broadcast = '';
        lastOctet = 255;
        
        for (i=0;i<4;++i){  
            network[i]=parseInt(network[i]);
            subnet[i]=parseInt(subnet[i]);
            
            // This octet must be 0 of the previous was less than 255.
            if ( lastOctet < 255 && subnet[i] !=0 )
                return -201; // Error
            
            // Determine if this octet's mask is valid
            if (!validOctetMask(subnet[i]))
                return -202; // Error
            
            if (subnet[i]==0) {           // Obvious ones
                broadcast += '255';  
            } else if (subnet[i]==255){   // Obvious ones
                broadcast += network[i]; 
            } else if (subnet[i]==254 ){    // Weird one
                if (i==3)
                    return -203;

                broadcast += network[i] + Math.abs(network[i]%2-1) ;
            } else {
                // The topmost address (Broadcast, obviously) from this subnet. 
                HOSTSPERSUBNET=255-subnet[i]
                TOTALSUBNETS = 255/HOSTSPERSUBNET
                
                broadcast += network[i]+HOSTSPERSUBNET;   
            }
            if (i<3) broadcast += '.';
            lastOctet = subnet[i];            
        }
        return broadcast;
    }

